/*
 * Improbability Drive
 * Phillip Cardon, Thach Nguyen, Cristopher Claeys
 * 4/26/2011
 */

package buffers;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;


/**
 * SyncronizedBuffer
 * Buffer for use by multiple threads, implemented as a queue.
 * @author Phillip Cardon
 *
 */
public class SynchronizedBuffer<T> {
	/**
	 * List object to hold pages.
	 */
	List<T> myQueue;
	
	public SynchronizedBuffer () {
		myQueue = Collections.synchronizedList(new LinkedList<T>());
		
	}
	
	/**
	 * Add element to queue
	 * @param element to add.
	 * @return element added.
	 */
	public boolean enqueue (T element) {
		return myQueue.add(element);
	}
	
	/**
	 * Size
	 * @return size of queue.
	 */
	public int size () {
		return myQueue.size();
	}
	
	/**
	 * dequeue element from queue.
	 * @return element from head of queue.
	 */
	public T dequeue() {
		if (size() == 0) {
			throw new NoSuchElementException();
		}
		return myQueue.remove(0);
	}
	
}
